<?php

require_once '../application/models/DB.php';

class Admin_Model_Account extends Application_Model_DB {

    public function authAdmin($login, $password, $remember, $host) {

        if ($this->isAuthUsers($login, $password)) {
            $this->onLogin($login, $password, $remember, $host);
            $result = true;
        } else {
            $result = false;
        }

        return $result;
    }
    
    public function getUserInfo($admin_account) {
        $AUM = new Admin_Model_AdminUsers();
        $user = $AUM->getUsersByLogin($admin_account->login);
        if (is_array($user) && count($user)) {
            $admin_account->id = $user['id'];
            $admin_account->role = $user['role'];
            $admin_account->role_id = $user['role_id'];
            $admin_account->time_zone = $user['time_zone'];
            $admin_account->locale = $user['locale'];
        }
    }

    function isAuthUsers($login, $password) {
        $sql = 'SELECT `id` FROM `admin_users` Where `login` = \'' . $login . '\' AND `password` = \'' . $password . '\'';
        $result = $this->GetOne($sql);
        if ($result) {
            return $result;
        }
        return false;
    }

    static public function CryptString($str) {
        $salt = 'jwe43n67nmwllw47658';
        return md5($str . $salt);
    }

    public function onLogin($login, $password, $remember, $host) {

        $admin_account = new Zend_Session_Namespace('admin_account');
        $admin_account->auth = true;
        $admin_account->login = $login;

        if (!empty($remember)) {
            $exp_date = time() + 60 * 60 * 24 * 31 * 12;
            setcookie('_lgn_', $login, $exp_date, '/', str_replace('www.', '', $host));
            setcookie('_pwd_', $password, $exp_date, '/', str_replace('www.', '', $host));
        }
    }

    final protected function getUserIp() {
        if (isset($HTTP_SERVER_VARS)) {
            if (isset($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])) {
                $realip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
            } elseif (isset($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])) {
                $realip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
            } else {
                $realip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
            }
        } else {
            if (getenv('HTTP_X_FORWARDED_FOR')) {
                $realip = getenv('HTTP_X_FORWARDED_FOR');
            } elseif (getenv('HTTP_CLIENT_IP')) {
                $realip = getenv('HTTP_CLIENT_IP');
            } else {
                $realip = getenv('REMOTE_ADDR');
            }
        }
        return $realip;
    }
    
    static function getRealTime($timeobj, $timezone, $locale, $format = 'Y-m-d H:i') {
        if (is_object($timeobj)) {
            if ($timeobj->format('Y-m-d H:i') == '-0001-11-30 00:00')
                return '';
            else
                return Application_Model_Date::getTimeObjectRelativelyUserTimeZoneOrUserLocale($timeobj, $timezone, $locale)->format($format);
        }
        return '';
    }

}